$(document).ready(function() {
  var baseUrl = 'http://192.168.1.41:8081'

  var params = {
    pageNum: 0,
    pageSize: 10,
    param: {
      corpAddr: '',
      corpName: $('#name').val()
    }
  }

  // 获取时间
  $('#time').text(getCurrentDate())

  // 轮播图数据
  getSwiperData()

  // 初始化搜索全部
  search()

  // 点击搜索
  $('#search').click(function() {
    search()
  })

  //  搜索框有内容时的操作
  $('#name').bind('input propertychange', function(event) {
    handleSearchInput()
  })

  // 清除搜索框内容
  $('.clear').click(function() {
    handleClear()
  })

  // 点击搜索
  function search() {
    params = {
      pageNum: 0,
      pageSize: 10,
      param: {
        corpAddr: '',
        corpName: $('#name').val()
      }
    }
    $('#list').html('')
    $('.dropload-down').remove()
    handleLoad()
  }

  //   获取轮播图数据
  function getSwiperData() {
    $.ajax({
      url: baseUrl + '/sys/slide/queryAllSlide',
      type: 'POST',
      dataType: 'json',
      success: function(data) {
        if (data.code == 200) {
          var res = data.result
          var swiperHtml = ''
          for (var index = 0; index < res.length; index++) {
            const element = res[index]
            swiperHtml +=
              '<div class="swiper-slide"><img src="http://pic.henanjingchi.com/ppd/original/' +
              element.slideSrc +
              '?x-oss-process=image/resize,m_lfit,h_120,w_120" alt=""></div>'
          }
          $('#swiper').html(swiperHtml)
          // 初始化轮播图
          var mySwiper = new Swiper('.swiper-container', {
            initialSlide: 0,
            observer: true,
            observeParents: true,
            loop: true,
            autoplay: {
              delay: 2000,
              disableOnInteraction: false //解决滑动后不能轮播的问题
            }, //可选选项，自动滑动
            pagination: {
              el: '.swiper-pagination'
            }
          })
        }
      }
    })
  }

  // 成功回调
  function successcb(data) {
    if (data.code == 200) {
      pages = data.result.pages
      var res = data.result.list
      var listHtml = buildListHtml(res)
      $('#list').html(listHtml)
    }
  }

  // 组装公司html
  function buildListHtml(res) {
    var listHtml = ''
    for (var i = 0; i < res.length; i++) {
      var company = res[i]
      var tableHtml = ''
      for (var j = 0; j < company.productPriceList.length; j++) {
        const product = company.productPriceList[j]
        var price = product.price ? product.price : '面议'
        tableHtml +=
          '<tr><td>' +
          product.productName +
          '</td><td>' +
          product.productType +
          '</td><td>' +
          price +
          '</td></tr>'
      }
      var addr = (
        (company.corp.corpAddr || '') + (company.corp.detailAddr || '')
      ).replace(/\//g, '')
      var introduce = company.corp.introduce || ''
      // var introduce = company.corp.introduce
      //   ? company.corp.introduce.length > 80
      //     ? company.corp.introduce.substr(0, 80) + '...'
      //     : company.corp.introduce.length
      //   : ''
      listHtml +=
        '<li class="item">' +
        '<div class="i-header">' +
        '<div class="header-img">' +
        '<img src="http://pic.henanjingchi.com/ppd/original/' +
        company.corp.logoPic +
        '?x-oss-process=image/resize,m_lfit,h_120,w_120" />' +
        '</div>' +
        '<div class="header-info">' +
        '<div class="info-item">公司名称：' +
        company.corp.corpName +
        '</div>' +
        '<div class="info-item">公司地址：' +
        addr +
        '</div>' +
        '<div class="info-item">地图位置：' +
        `<a onclick="handleMap('${addr}')">点击这里</a>' +
        '</div>' +
        '<div class="info-item">公司介绍：' +
        introduce +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div class="i-table">' +
        '<table class="table" border="1" cellspacing="0">' +
        '<tr>' +
        '<th>产品类型</th>' +
        '<th>产品名</th>' +
        '<th>价格（元）</th>' +
        '</tr>' +
        tableHtml +
        '</table>' +
        '</div>' +
        '<div class="i-footer">' +
        '<div class="footer-info">' +
        '<svg t="1591105761267" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1936" width="12" height="12"><path d="M1004.8 1024H19.2c0-230.4 160-422.4 371.2-480-108.8-38.4-185.6-140.8-185.6-262.4C204.8 128 332.8 0 492.8 0s281.6 128 281.6 281.6c0 115.2-70.4 211.2-166.4 256 224 44.8 396.8 249.6 396.8 486.4z" p-id="1937"></path></svg>' +
        '<span>业务联系人：' +
        company.corp.busiLinkman +
        '</span>' +
        '</div>' +
        '<div class="footer-info">' +
        '<svg t="1591105983839" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2665" width="12" height="12"><path d="M621.034 712.463c1.863-1.156 22.295-13.942 28.27-17.54 11.052-6.683 20.175-11.694 29.299-15.806 64.122-29.17 120.663-8.546 195.451 78.836 46.261 54.1 59.946 102.095 42.085 144.821-13.3 31.933-38.936 53.971-84.555 81.214-2.955 1.734-29.234 17.026-35.98 21.203-106.207 65.536-349.654-84.49-524.738-340.852C95.332 407.335 49.906 133.305 157.527 66.805l14.2-8.995 15.42-10.023c54.42-35.21 89.245-49.28 130.558-42.277 41.25 7.067 76.33 37.586 106.014 93.163 62.58 117.58 48.767 173.35-31.74 224.044-5.782 3.726-26.343 16.127-28.27 17.347-19.725 12.208 13.685 94 91.365 207.724 78.643 115.01 144.372 177.91 165.896 164.675z" p-id="2666"></path></svg>' +
        '<span>业务联系人：' +
        company.corp.busiTel +
        '</span>' +
        '</div>' +
        '</div>' +
        '</li>'
    }
    return listHtml
  }

  function errorcb(err) {
    console.log(err)
  }

  // 获取公司数据
  function getCompanyData(successcb, errorcb) {
    $.ajax({
      url: baseUrl + '/busi/corp/corp4Price',
      type: 'POST',
      data: JSON.stringify(params),
      contentType: 'application/json',
      dataType: 'json',
      success: function(data) {
        if (typeof successcb === 'function') {
          successcb(data)
        }
      },
      error: function(err) {
        if (typeof errorcb === 'function') {
          errorcb(err)
        }
      }
    })
  }

  //   获取当前日期
  function getCurrentDate() {
    var now = new Date()
    var year = now.getFullYear()
    var month = now.getMonth()
    var date = now.getDate()
    month = month + 1
    if (month < 10) month = '0' + month
    if (date < 10) date = '0' + date
    var time = ''
    time = year + '年' + month + '月' + date + '日'
    return time
  }



  // 输入时 显示清除按钮
  function handleSearchInput() {
    if($('#name').val()){
      $('.clear').show()
    }
  }
  
  // 清除搜索框
  function handleClear() {
    $('#name').val('')
    $('.clear').hide()
  }

  // 下拉刷新 上拉加载
  function handleLoad() {
    // dropload
    $('.app').dropload({
      scrollArea: window,
      domUp: {
        domClass: 'dropload-up',
        domRefresh: '<div class="dropload-refresh">↓下拉刷新</div>',
        domUpdate: '<div class="dropload-update">↑释放更新</div>',
        domLoad:
          '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
      },
      domDown: {
        domClass: 'dropload-down',
        domRefresh: '<div class="dropload-refresh">↑上拉加载更多</div>',
        domLoad:
          '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
        domNoData: '<div class="dropload-noData">暂无数据</div>'
      },
      loadUpFn: function(me) {
        params = {
          pageNum: 0,
          pageSize: 10,
          param: {
            corpAddr: '',
            corpName: $('#name').val()
          }
        }
        var loadUpFnSuccesscb = function(data) {
          successcb(data)
          me.resetload()
          params.sizeNum = 1
          me.unlock()
          me.noData(false)
        }
        var loadUpFnErrorcb = function(err) {
          errorcb(err)
          me.resetload()
        }

        getCompanyData(loadUpFnSuccesscb, loadUpFnErrorcb)
      },
      loadDownFn: function(me) {
        params.pageNum++
        var loadDownFnSuccesscb = function(data) {
          if (data.code == 200) {
            var listHtml = ''
            var res = data.result.list
            if (res.length > 0) {
              listHtml = buildListHtml(res)
            } else {
              me.lock()
              me.noData()
            }
            $('#list').append(listHtml)
            me.resetload()
          }
        }
        var loadDownFnErrorcb = function(err) {
          errorcb(err)
          me.resetload()
        }

        getCompanyData(loadDownFnSuccesscb, loadDownFnErrorcb)
      },
      threshold: 50
    })
  }
})

function handleMap(addr) {
  window.location.href = 'baidumap://map/place/search?query=' + addr
  setTimeout(function() {
    window.location.href = 'html/map.html?addr=' + addr
  }, 200)
}
